package com.shsh.safemanage.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.shsh.safemanage.dao.DaoCreator;
import com.shsh.safemanage.dao.DepartmentDao;
import com.shsh.safemanage.dao.EquipmentDao;
import com.shsh.safemanage.dao.PostCheckDao;
import com.shsh.safemanage.dao.TitlePostManageGroupDao;
import com.shsh.safemanage.dao.UserDao;
import com.shsh.safemanage.model.Department1;
import com.shsh.safemanage.model.Department2;
import com.shsh.safemanage.model.Department3;
import com.shsh.safemanage.model.Equipment;
import com.shsh.safemanage.model.JobPost;
import com.shsh.safemanage.model.ManageGroup;
import com.shsh.safemanage.model.PostCheck;
import com.shsh.safemanage.model.ProblemTransferFeedback;
import com.shsh.safemanage.model.Role;
import com.shsh.safemanage.model.User;
import com.shsh.safemanage.util.Constants;
import com.shsh.safemanage.util.MessageUtil;
import com.shsh.safemanage.util.Util;

public class UserAction {
	public Boolean login(HttpServletRequest request) {
		String jobNum = request.getParameter(Constants.REQUEST_PARAMETER_JOB_NUM);
		String password = request.getParameter(Constants.REQUEST_PARAMETER_PASSWORD);

		UserDao userDao = DaoCreator.createUserDao();
		User user = userDao.login(jobNum, password);

		if (user == null) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_LOGIN_FAILED);
			return false;
		} else {
			HttpSession session = request.getSession();
			session.setAttribute(Constants.SESSION_USER, user);

			return true;
		}
	}

	public boolean refreshUserInfo(HttpServletRequest request) {
		String nameStr = request.getParameter(Constants.REQUEST_PARAMETER_NAME);
		String jobNumStr = request.getParameter(Constants.REQUEST_PARAMETER_JOB_NUM);

		if ((nameStr == null || nameStr.length() == 0) && (jobNumStr == null || jobNumStr.length() == 0)) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_EMPTY_USERNAME_OR_JOBNUM);
			return false;
		}

		HttpSession session = request.getSession();

		UserDao userDao = DaoCreator.createUserDao();
		User user = userDao.getUser(nameStr, jobNumStr);
		if (user == null) {
			session.setAttribute(Constants.SESSION_REGISTER_USER, null);
			return true;
		}

		session.setAttribute(Constants.SESSION_REGISTER_USER, user);

		return true;
	}

	public boolean refreshUserInfo(HttpServletRequest request, String attributeKey) {
		String nameStr = request.getParameter(Constants.REQUEST_PARAMETER_NAME);
		String jobNumStr = request.getParameter(Constants.REQUEST_PARAMETER_JOB_NUM);

		if ((nameStr == null || nameStr.length() == 0) && (jobNumStr == null || jobNumStr.length() == 0)) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_EMPTY_USERNAME_OR_JOBNUM);
			return false;
		}

		UserDao userDao = DaoCreator.createUserDao();
		User user = userDao.getUser(nameStr, jobNumStr);

		request.getSession().setAttribute(attributeKey, user);

		return true;
	}

	public boolean chooseMappingShiyebu(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Set<Department1> set = (Set<Department1>) session.getAttribute(Constants.SESSION_MAPPING_SHIYEBU);
		if (set == null) {
			set = new HashSet<Department1>();
			session.setAttribute(Constants.SESSION_MAPPING_SHIYEBU, set);
		}

		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
			Department1 department1 = departmentDao.getDepartment1ByID(id);
			set.add(department1);

			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean deleteMappingShiyebu(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Set<Department1> set = (Set<Department1>) session.getAttribute(Constants.SESSION_MAPPING_SHIYEBU);
		if (set == null) {
			return true;
		}

		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			for (Department1 depart1 : set) {
				if (depart1.getId().equals(id)) {
					set.remove(depart1);
					break;
				}
			}

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean chooseMappingEquipment1(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Set<Equipment> set = (Set<Equipment>) session.getAttribute(Constants.SESSION_MAPPING_EQUIPMENT);
		if (set == null) {
			set = new HashSet<Equipment>();
			session.setAttribute(Constants.SESSION_MAPPING_EQUIPMENT, set);
		}

		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			EquipmentDao equipmentDao = DaoCreator.createEquipmentDao();
			Equipment equipment = equipmentDao.getEquipment(id);
			set.add(equipment);

			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean deleteMappingEquipment1(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Set<Equipment> set = (Set<Equipment>) session.getAttribute(Constants.SESSION_MAPPING_EQUIPMENT);
		if (set == null) {
			return true;
		}

		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			for (Equipment equip : set) {
				if (equip.getId().equals(id)) {
					set.remove(equip);
					break;
				}
			}

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean chooseMappingEquipment2(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Set<Equipment> set = (Set<Equipment>) session.getAttribute(Constants.SESSION_MAPPING_EQUIPMENT2);
		if (set == null) {
			set = new HashSet<Equipment>();
			session.setAttribute(Constants.SESSION_MAPPING_EQUIPMENT2, set);
		}

		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		String isManagerStr = request.getParameter(Constants.REQUEST_PARAMETER_IS_MANAGER);
		try {
			Long id = new Long(idStr);
			EquipmentDao equipmentDao = DaoCreator.createEquipmentDao();
			Equipment equipment = equipmentDao.getEquipment(id);
			if (isManagerStr != null && isManagerStr.equals("1")) {
				equipment.setEquipmentManager(true);
			} else {
				equipment.setEquipmentManager(false);
			}
			set.add(equipment);

			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean chooseMappingManageGroup(HttpServletRequest request) {
		HttpSession session = request.getSession();

		Set<ManageGroup> manageGroupSet = (Set<ManageGroup>) session.getAttribute(Constants.SESSION_MANAGE_GROUP_SET);
		if (manageGroupSet == null) {
			manageGroupSet = new HashSet<ManageGroup>();
			session.setAttribute(Constants.SESSION_MANAGE_GROUP_SET, manageGroupSet);
		}

		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			TitlePostManageGroupDao titlePostManageGroupDao = DaoCreator.createTitlePostManageGroupDao();
			ManageGroup manageGroup = titlePostManageGroupDao.getManageGroupByID(id);

			manageGroupSet.add(manageGroup);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean chooseMappingManageGroup2(HttpServletRequest request) {
		String manageGroupIDs = request.getParameter(Constants.REQUEST_PARAMETER_MANAGEGROUP_ID);
		String depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID);

		if (Util.isNullOrEmpty(manageGroupIDs)) {
			return true;
		}

		HttpSession session = request.getSession();
		Set<ManageGroup> manageGroupSet = (Set<ManageGroup>) session.getAttribute(Constants.SESSION_MANAGE_GROUP_SET);
		if (manageGroupSet == null) {
			manageGroupSet = new HashSet<ManageGroup>();
			session.setAttribute(Constants.SESSION_MANAGE_GROUP_SET, manageGroupSet);
		}

		User registerUser = (User) session.getAttribute(Constants.SESSION_REGISTER_USER);

		Long depart1ID = 0L;
		try {
			depart1ID = new Long(depart1IDStr);
		} catch (Exception e) {
			depart1ID = registerUser.getDepartment1ID();
		}

		String[] manageGroupIDStrArr = manageGroupIDs.split(",");
		try {
			TitlePostManageGroupDao titlePostManageGroupDao = DaoCreator.createTitlePostManageGroupDao();
			for (String manageGroupIDStr : manageGroupIDStrArr) {
				Long manageGroupID = new Long(manageGroupIDStr);
				ManageGroup manageGroup = titlePostManageGroupDao.getManageGroupByID(manageGroupID);
				manageGroup.setDepart1ID(depart1ID);

				manageGroupSet.add(manageGroup);
			}

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean deleteMappingManageGroup(HttpServletRequest request) {
		HttpSession session = request.getSession();

		Set<ManageGroup> manageGroupSet = (Set<ManageGroup>) session.getAttribute(Constants.SESSION_MANAGE_GROUP_SET);
		if (manageGroupSet == null) {
			return true;
		}

		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		String depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID);
		try {
			Long id = new Long(idStr);
			Long depart1ID = new Long(depart1IDStr);
			for (ManageGroup manageGroup : manageGroupSet) {
				if (manageGroup.getId().equals(id) && manageGroup.getDepart1ID().equals(depart1ID)) {
					manageGroupSet.remove(manageGroup);
					break;
				}
			}

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean chooseMappingPostcheck(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Set<PostCheck> set = (Set<PostCheck>) session.getAttribute(Constants.SESSION_MAPPING_POSTCHECK_SET);
		if (set == null) {
			set = new HashSet<PostCheck>();
			session.setAttribute(Constants.SESSION_MAPPING_POSTCHECK_SET, set);
		}

		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			PostCheckDao postCheckDao = DaoCreator.createPostCheckDao();
			PostCheck postCheck = postCheckDao.getPostCheck(id);
			set.add(postCheck);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean chooseMappingPostcheck2(HttpServletRequest request) {
		String postcheckIDs = request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_ID);

		if (Util.isNullOrEmpty(postcheckIDs)) {
			return true;
		}

		HttpSession session = request.getSession();
		Set<PostCheck> set = (Set<PostCheck>) session.getAttribute(Constants.SESSION_MAPPING_POSTCHECK_SET);
		if (set == null) {
			set = new HashSet<PostCheck>();
			session.setAttribute(Constants.SESSION_MAPPING_POSTCHECK_SET, set);
		}

		String[] postcheckIDStrArr = postcheckIDs.split(",");
		try {
			PostCheckDao postCheckDao = DaoCreator.createPostCheckDao();
			for (String postcheckIDStr : postcheckIDStrArr) {
				Long postcheckID = new Long(postcheckIDStr);
				PostCheck postcheck = postCheckDao.getPostCheck(postcheckID);

				set.add(postcheck);
			}

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean deleteMappingPostcheck(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Set<PostCheck> set = (Set<PostCheck>) session.getAttribute(Constants.SESSION_MAPPING_POSTCHECK_SET);
		if (set == null) {
			return true;
		}

		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			for (PostCheck postCheck : set) {
				if (postCheck.getId().equals(id)) {
					set.remove(postCheck);
					break;
				}
			}

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean chooseDepartment123(HttpServletRequest request) {
		HttpSession session = request.getSession();
		User registerUser = (User) session.getAttribute(Constants.SESSION_REGISTER_USER);
		if (registerUser == null) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_REGISTER_USER);
			return false;
		}

		String depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID);
		String depart2IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT2ID);
		String depart3IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT3ID);

		DepartmentDao departmentDao = DaoCreator.createDepartmentDao();

		try {
			Long depart1ID = new Long(depart1IDStr);
			Long depart2ID = new Long(depart2IDStr);
			Long depart3ID = new Long(depart3IDStr);

			Department1 department1 = departmentDao.getDepartment1ByID(depart1ID);
			if (department1 == null) {
				request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_REGISTER_USER);
				return false;
			}

			Department2 department2 = departmentDao.getDepartment2ByID(depart2ID);
			if (department2 == null) {
				request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_REGISTER_USER);
				return false;
			}

			Department3 department3 = departmentDao.getDepartment3ByID(depart3ID);
			if (department3 == null) {
				request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_REGISTER_USER);
				return false;
			}

			registerUser.setDepartment1ID(depart1ID);
			registerUser.setDepartment1Name(department1.getDepartmentName());
			registerUser.setDepartment2ID(depart2ID);
			registerUser.setDepartment2Name(department2.getDepartment2Name());
			registerUser.setDepartment3ID(depart3ID);
			registerUser.setDepartment3Name(department3.getDepartment3Name());
			registerUser.setModified(true);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean choosePost(HttpServletRequest request) {
		HttpSession session = request.getSession();

		User registerUser = (User) session.getAttribute(Constants.SESSION_REGISTER_USER);
		if (registerUser == null) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_REGISTER_USER);
			return false;
		}

		TitlePostManageGroupDao titlePostManageGroupDao = DaoCreator.createTitlePostManageGroupDao();

		String postIDStr = request.getParameter(Constants.REQUEST_PARAMETER_POSTID);
		try {
			Long postID = new Long(postIDStr);

			JobPost jobPost = titlePostManageGroupDao.getJobPostByID(postID);
			if (jobPost == null) {
				return false;
			}

			if (registerUser.getJobPost1ID() == null || registerUser.getJobPost1ID().equals(0L)) {
				registerUser.setJobPost1ID(postID);
				registerUser.setJobPost1Name(jobPost.getPostName());

				registerUser.setModified(true);
			} else if (registerUser.getJobPost2ID() == null || registerUser.getJobPost2ID().equals(0L)) {
				if (registerUser.getJobPost1ID().equals(postID)) {
					return true;
				}
				registerUser.setJobPost2ID(postID);
				registerUser.setJobPost2Name(jobPost.getPostName());

				registerUser.setModified(true);
			} else if (registerUser.getJobPost3ID() == null || registerUser.getJobPost3ID().equals(0L)) {
				if (registerUser.getJobPost1ID().equals(postID) || registerUser.getJobPost2ID().equals(postID)) {
					return true;
				}
				registerUser.setJobPost3ID(postID);
				registerUser.setJobPost3Name(jobPost.getPostName());

				registerUser.setModified(true);
			}

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean addReadProblemID(HttpServletRequest request) {
		HttpSession session = request.getSession();
		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		
		try {
			Long id = new Long(idStr);
			if (id > 0) {
				List<Long> problemIDList = (List<Long>) session.getAttribute(Constants.SESSION_READ_PROBLEM_ID_LIST);
				if (problemIDList == null) {
					problemIDList = new ArrayList<Long>();
					session.setAttribute(Constants.SESSION_READ_PROBLEM_ID_LIST, problemIDList);
				}
				
				problemIDList.add(id);
			}
		} catch (Exception e) {
		}
		
		return true;
	}
	
	public boolean isProblemBeRead(HttpServletRequest request, Long problemID) {
		HttpSession session = request.getSession();
		List<Long> problemIDList = (List<Long>) session.getAttribute(Constants.SESSION_READ_PROBLEM_ID_LIST);
		if (problemIDList == null) {
			return false;
		}
		
		if (problemIDList.indexOf(problemID) != -1) {
			return true;
		}
		
		return false;
	}

	public boolean register(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String password = request.getParameter(Constants.REQUEST_PARAMETER_PASSWORD);
		String confirmPassword = request.getParameter(Constants.REQUEST_PARAMETER_CONFIRM_PASSWORD);

		if (password == null || confirmPassword == null) {
			return false;
		}

		if (!password.equals(confirmPassword)) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NOT_EQUAL_PASSWORDS);
			return false;
		}

		User registerUser = (User) session.getAttribute(Constants.SESSION_REGISTER_USER);
		registerUser.setPassword(password);
		UserDao userDao = DaoCreator.createUserDao();
		boolean isSuccess = userDao.updateUserInfo(registerUser);
		if (isSuccess) {
			registerUser.setModified(false);
			registerUser.setRegistered(true);
		}

		// update shiyebu mapping
		Set<Department1> department1Set = (Set<Department1>) session.getAttribute(Constants.SESSION_MAPPING_SHIYEBU);
		if (department1Set != null && department1Set.size() > 0) {
			DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
			departmentDao.addUserDepartment1Mapping(registerUser, department1Set);
		}

		// update equipment mapping
		Set<Equipment> equipmentSet = (Set<Equipment>) session.getAttribute(Constants.SESSION_MAPPING_EQUIPMENT);
		if (equipmentSet != null && equipmentSet.size() > 0) {
			EquipmentDao equipmentDao = DaoCreator.createEquipmentDao();
			equipmentDao.addUserEquipmentMapping(registerUser, equipmentSet);
		}

		// update equipment1 mapping
		Set<Equipment> equipment2Set = (Set<Equipment>) session.getAttribute(Constants.SESSION_MAPPING_EQUIPMENT2);
		if (equipment2Set != null && equipment2Set.size() > 0) {
			EquipmentDao equipmentDao = DaoCreator.createEquipmentDao();
			equipmentDao.addUserEquipmentMapping(registerUser, equipment2Set);
		}

		// update postcheck mapping
		Set<PostCheck> postCheckSet = (Set<PostCheck>) session.getAttribute(Constants.SESSION_MAPPING_POSTCHECK_SET);
		if (postCheckSet != null && postCheckSet.size() > 0) {
			PostCheckDao postCheckDao = DaoCreator.createPostCheckDao();
			postCheckDao.addUserPostcheckMapping(registerUser, postCheckSet);

			if (registerUser.getManageGroupID() != null) {
				TitlePostManageGroupDao titlePostManageGroupDao = DaoCreator.createTitlePostManageGroupDao();
				titlePostManageGroupDao.addManagegroupPostcheckMapping(registerUser.getManageGroupID(), postCheckSet);
			}
		}

		session.setAttribute(Constants.SESSION_REGISTER_USER, null);

		return true;
	}

	public boolean register1(HttpServletRequest request, boolean isModify) {
		HttpSession session = request.getSession();

		String jobNumStr = request.getParameter(Constants.REQUEST_PARAMETER_JOB_NUM);
		String nameStr = request.getParameter(Constants.REQUEST_PARAMETER_NAME);
		String depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID);
		String depart2IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT2ID);
		String depart3IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT3ID);

		if (Util.isNullOrEmpty(jobNumStr) || Util.isNullOrEmpty(nameStr) || Util.isNullOrEmpty(depart1IDStr)
				|| Util.isNullOrEmpty(depart2IDStr) || Util.isNullOrEmpty(depart3IDStr)) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_EMPTY_USERNAME_OR_JOBNUM);
			return false;
		}

		if (!Util.isNumeric(jobNumStr)) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_INVALID_WORKNUM);
			return false;
		}

		if (jobNumStr.length() != 8) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_INVALID_WORKNUM);
			return false;
		}

		nameStr = Util.isoToUTF8(nameStr);
		if (!Util.isChinese(nameStr)) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_INVALID_NAME);
			return false;
		}

		UserDao userDao = DaoCreator.createUserDao();
		if (!isModify && userDao.hasBeenRegistered(jobNumStr)) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_USER_HAS_REGISTERED);
			return false;
		}

		Long depart1ID = 0L;
		Long depart2ID = 0L;
		Long depart3ID = 0L;
		try {
			depart1ID = new Long(depart1IDStr);
			depart2ID = new Long(depart2IDStr);
			depart3ID = new Long(depart3IDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_INVALID_DEPARTMENT_KEY);
			return false;
		}

		DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
		Department1 department1 = departmentDao.getDepartment1ByID(depart1ID);
		Department2 department2 = departmentDao.getDepartment2ByID(depart2ID);
		Department3 department3 = departmentDao.getDepartment3ByID(depart3ID);
		if (department1 == null || department2 == null || department3 == null) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_EMPTY_USERNAME_OR_JOBNUM);
			return false;
		}

		User registerUser = (User) session.getAttribute(Constants.SESSION_REGISTER_USER);
		if (registerUser == null) {
			registerUser = new User();
			session.setAttribute(Constants.SESSION_REGISTER_USER, registerUser);
		}

		registerUser.setName(nameStr);
		registerUser.setJobNum(jobNumStr);
		registerUser.setDepartment1ID(depart1ID);
		registerUser.setDepartment2ID(depart2ID);
		registerUser.setDepartment3ID(depart3ID);

		TitlePostManageGroupDao tpmDao = DaoCreator.createTitlePostManageGroupDao();
		Role role = tpmDao.getRoleByID(department3.getRoleID());
		if ((role.getRoleType() & Constants.ROLE_TYPE_YIXIANYUANGONG) > 0) {
			session.setAttribute(Constants.SESSION_REGISTER_NEXT, "1");

			if (registerUser.getPassword() == null) {
				registerUser.setPassword("");
			}
			userDao.updateUserInfo(registerUser);

			session.removeAttribute(Constants.SESSION_REGISTER_NEXT);
			session.removeAttribute(Constants.SESSION_REGISTER_USER);
			session.removeAttribute(Constants.SESSION_USER);
		} else if ((role.getRoleType() & (Constants.ROLE_TYPE_ANQUANZONGJIAN | Constants.ROLE_TYPE_FUKEZHANG | Constants.ROLE_TYPE_KEYUAN
				| Constants.ROLE_TYPE_KEZHANG | Constants.ROLE_TYPE_ZHUANGZHIZHANG | Constants.ROLE_TYPE_ZUYUAN | Constants.ROLE_TYPE_ZUZHANG)) > 0) {
			session.setAttribute(Constants.SESSION_REGISTER_NEXT, "2");
		} else if ((role.getRoleType() & (Constants.ROLE_TYPE_SHENGCHANFUBUZHANG | Constants.ROLE_TYPE_SHEBEIFUBUZHANG
				| Constants.ROLE_TYPE_BUZHANG | Constants.ROLE_TYPE_FUZONGJINGLI | Constants.ROLE_TYPE_ZONGJINGLI
				| Constants.ROLE_TYPE_ZONGSHI | Constants.ROLE_TYPE_CHUZHANG | Constants.ROLE_TYPE_FUCHUZHANG)) > 0) {
			session.setAttribute(Constants.SESSION_REGISTER_NEXT, "3");
		}

		return true;
	}

	public boolean register2(HttpServletRequest request) {
		HttpSession session = request.getSession();

		User registerUser = (User) session.getAttribute(Constants.SESSION_REGISTER_USER);
		if (registerUser == null) {
			return false;
		}

		String password = request.getParameter(Constants.REQUEST_PARAMETER_PASSWORD);
		String confirmPassword = request.getParameter(Constants.REQUEST_PARAMETER_CONFIRM_PASSWORD);

		if (password == null || confirmPassword == null) {
			return false;
		}

		if (!password.equals(confirmPassword)) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NOT_EQUAL_PASSWORDS);
			return false;
		}

		registerUser.setPassword(password);

		UserDao userDao = DaoCreator.createUserDao();
		boolean isSuccess = true;

		isSuccess = userDao.updateUserInfo(registerUser);

		if (!isSuccess) {
			return false;
		}

		// update equipment mapping
		Set<Equipment> equipmentSet = (Set<Equipment>) session.getAttribute(Constants.SESSION_MAPPING_EQUIPMENT);
		EquipmentDao equipmentDao = DaoCreator.createEquipmentDao();
		equipmentDao.deleteUserMappingEquipments(registerUser.getId());
		if (equipmentSet != null && equipmentSet.size() > 0) {
			isSuccess = equipmentDao.addUserEquipmentMapping(registerUser, equipmentSet);
		}

		session.removeAttribute(Constants.SESSION_MAPPING_EQUIPMENT);

		if (!isSuccess) {
			return false;
		}

		// update managegroup mapping
		Set<ManageGroup> managegroupSet = (Set<ManageGroup>) session.getAttribute(Constants.SESSION_MANAGE_GROUP_SET);
		TitlePostManageGroupDao titlePostManageGroupDao = DaoCreator.createTitlePostManageGroupDao();
		titlePostManageGroupDao.deleteUserMappingManageGroups(registerUser.getId());
		if (managegroupSet != null && managegroupSet.size() > 0) {
			isSuccess = titlePostManageGroupDao.addUserManagegroupMapping(registerUser, managegroupSet);
		}

		session.removeAttribute(Constants.SESSION_MANAGE_GROUP_SET);

		if (!isSuccess) {
			return false;
		}

		// update postcheck mapping
		Set<PostCheck> postCheckSet = (Set<PostCheck>) session.getAttribute(Constants.SESSION_MAPPING_POSTCHECK_SET);
		PostCheckDao postCheckDao = DaoCreator.createPostCheckDao();
		postCheckDao.deleteUserMappingPostchecks(registerUser.getId());
		if (postCheckSet != null && postCheckSet.size() > 0) {
			isSuccess = postCheckDao.addUserPostcheckMapping(registerUser, postCheckSet);
		}

		session.removeAttribute(Constants.SESSION_MAPPING_POSTCHECK_SET);

		if (!isSuccess) {
			return false;
		}

		// update shiyebu mapping
		Set<Department1> depart1Set = (Set<Department1>) session.getAttribute(Constants.SESSION_MAPPING_SHIYEBU);
		DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
		departmentDao.deleteUserMappingDepartment1s(registerUser.getId());
		if (depart1Set != null && depart1Set.size() > 0) {
			isSuccess = departmentDao.addUserDepartment1Mapping(registerUser, depart1Set);
		}

		session.removeAttribute(Constants.SESSION_MAPPING_SHIYEBU);

		session.removeAttribute(Constants.SESSION_REGISTER_NEXT);
		session.removeAttribute(Constants.SESSION_REGISTER_USER);
		session.removeAttribute(Constants.SESSION_USER);

		return isSuccess;
	}

	public Role getUserRole(HttpServletRequest request) {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Constants.SESSION_USER);
		DepartmentDao departDao = DaoCreator.createDepartmentDao();
		TitlePostManageGroupDao tpmDao = DaoCreator.createTitlePostManageGroupDao();

		Department3 department3 = departDao.getDepartment3ByID(user.getDepartment3ID());
		Role role = tpmDao.getRoleByID(department3.getRoleID());

		return role;
	}

	public Role getUserRole(User user) {
		DepartmentDao departDao = DaoCreator.createDepartmentDao();
		TitlePostManageGroupDao tpmDao = DaoCreator.createTitlePostManageGroupDao();

		Department3 department3 = departDao.getDepartment3ByID(user.getDepartment3ID());
		Role role = tpmDao.getRoleByID(department3.getRoleID());

		return role;
	}

	public PostCheck[] getPostcheckMapping(HttpServletRequest request) {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Constants.SESSION_USER);

		UserDao userDao = DaoCreator.createUserDao();

		return userDao.getPostcheckMappingByUserID(user.getId());
	}

	public String getUserTransferFeedbackStr(ProblemTransferFeedback feedback) {
		if (feedback == null) {
			return "&nbsp;";
		}

		UserDao userDao = DaoCreator.createUserDao();
		UserAction userAction = ActionCreator.createUserAction();

		User theFeedbackUser = userDao.getUserByID(feedback.getUserID());
		if (theFeedbackUser == null) {
			return "&nbsp;";
		}
		Role theFeedbackUserRole = userAction.getUserRole(theFeedbackUser);

		StringBuilder sb = new StringBuilder();
		sb.append("<strong>" + theFeedbackUser.getName() + "</strong>");
		if ((theFeedbackUserRole.getRoleType() == Constants.ROLE_TYPE_BUZHANG || theFeedbackUserRole.getRoleType() == Constants.ROLE_TYPE_CHUZHANG)
				&& (feedback.getProcessMethod() == Constants.PROCESS_METHOD_ESCALATE)) {
			sb.append(MessageUtil.getMessage(Constants.LABEL_KEY_AGREE));
		}
		sb.append(feedback.getProcessMethodStr());

		return sb.toString();
	}
}
